package com.byd.dms.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.byd.dms.entity.User;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class FastJsonDemo {
    
    public static void main(String[] args) {
        //创建一个对象
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        
        System.out.println("*******Java对象 转 JSON字符串*******");
        String str1 = JSON.toJSONString(list);
        System.out.println("JSON.toJSONString(list)==>" + str1);
        String str2 = JSON.toJSONString(user1);
        System.out.println("JSON.toJSONString(user1)==>" + str2);
        
        System.out.println("\n****** JSON字符串 转 Java对象*******");
        User jp_user1 = JSON.parseObject(str2, User.class);
        System.out.println("JSON.parseObject(str2,User.class)==>" + jp_user1);
        
        //JSON.parseObject(str1, List<User>.c)
        
        
        System.out.println("\n****** Java对象 转 JSON对象 ******");
        JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
        System.out.println("(JSONObject) JSON.toJSON(user2)==>" + jsonObject1.getString("name"));
        
        System.out.println("\n****** JSON对象 转 Java对象 ******");
        User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
        System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>" + to_java_user);
    }
    
    private String getMethodName() {
        return new Exception().getStackTrace()[1].getMethodName();
    }
    
    @Test
    public void obj2JSON() {
        System.out.println("*******Java对象 转 JSON字符串*******");
        User user1 = new User("秦疆1号", 3, "男");
        String str2 = JSON.toJSONString(user1);
        System.out.println(getMethodName() + ": " + str2);
    }
    
    @Test
    public void list2JSON() {
        System.out.println("*******Java对象 转 JSON字符串*******");
        //创建一个对象
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        
        String str1 = JSON.toJSONString(list);
        System.out.println(getMethodName() + ": " + str1);
        
    }
    
    @Test
    public void json2Bean() {
        System.out.println("\n****** JSON字符串 转 Java对象*******");
        String str2 = "{\"age\":3,\"name\":\"秦疆1号\",\"sex\":\"男\"}";
        
        User jp_user1 = JSON.parseObject(str2, User.class);
        System.out.println("JSON.parseObject(str2,User.class)==>" + jp_user1);
    }
    
    @Test
    public void bean2JSONObject() {
        System.out.println("\n****** Java对象 转 JSON对象 ******");
        User user2 = new User("秦疆2号", 3, "男");
        JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
        System.out.println("(JSONObject) JSON.toJSON(user2)==>" + jsonObject1.getString("name"));
        System.out.println("\n****** JSON对象 转 Java对象 JSONObject2Bean ******");
        User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
        System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>" + to_java_user);
        
    }
    
    @Test
    public void JSONObject2Bean() {
    }
}
